package com.hjq.fcloud.ui.activity

import android.app.Activity
import android.content.Intent
import android.os.Handler
import android.view.View
import com.hjq.fcloud.R
import com.hjq.fcloud.app.AppActivity
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack
import com.shuyu.gsyvideoplayer.player.IjkPlayerManager
import com.shuyu.gsyvideoplayer.player.PlayerFactory
import com.shuyu.gsyvideoplayer.player.SystemPlayerManager
import com.shuyu.gsyvideoplayer.utils.GSYVideoType
import com.shuyu.gsyvideoplayer.utils.OrientationUtils
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer

/**
 * 视频播放
 */
class VideoPlayerActivity : AppActivity() {

    companion object {
        const val INTENT_IN_URL = "url";
        fun start(mActivity: Activity, url: String) {
            var intent = Intent(mActivity, VideoPlayerActivity::class.java)
            intent.putExtra(INTENT_IN_URL, url)
            mActivity?.startActivity(intent)
        }
    }

    private var mHandler = Handler()
    private var url:String? = null
    var videoPlayer: StandardGSYVideoPlayer? = null
    var orientationUtils: OrientationUtils? = null

    override fun getLayoutId(): Int {
        return R.layout.activity_video_player
    }

    override fun initView() {
        url = getString(INTENT_IN_URL)
        initVideoPlayer()
    }

    override fun initData() {}

    private fun initVideoPlayer() {
        PlayerFactory.setPlayManager(SystemPlayerManager::class.java) //ijk内核，默认模式，可以播放
        GSYVideoType.enableMediaCodec() //使能硬解码，播放前设置

        videoPlayer = findViewById<View>(R.id.video_player) as StandardGSYVideoPlayer
        videoPlayer!!.setUp(url, true, "视频播放")

        //增加封面
//        val imageView = ImageView(this)
//        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
//        imageView.setImageResource(R.mipmap.ic_launcher)
//        videoPlayer!!.thumbImageView = imageView

        //增加title
        videoPlayer!!.titleTextView.visibility = View.VISIBLE
        //设置返回键
        videoPlayer!!.backButton.visibility = View.VISIBLE
        //设置旋转
        orientationUtils = OrientationUtils(this, videoPlayer)
        //设置全屏按键功能,这是使用的是选择屏幕，而不是全屏
        videoPlayer!!.fullscreenButton.setOnClickListener { // ------- ！！！如果不需要旋转屏幕，可以不调用！！！-------
            // 不需要屏幕旋转，还需要设置 setNeedOrientationUtils(false)
            mHandler.postDelayed({//视频播放这里不知道为什么，要延迟调用行，要不然频繁点击“切换横竖屏”按钮有时候会没反应
                orientationUtils?.resolveByClick();
            },200)
//            finish()
        }
        //是否可以滑动调整
        videoPlayer!!.setIsTouchWiget(true)
        //设置返回按键功能
        videoPlayer!!.backButton.setOnClickListener { onBackPressed() }

        videoPlayer!!.startPlayLogic()
        videoPlayer?.setVideoAllCallBack(object: GSYSampleCallBack() {
            override fun onAutoComplete(url: String?, vararg objects: Any?) {
                videoPlayer?.initUIState()//UI显示回到初始化状态
            }
        })
    }

    override fun onPause() {
        super.onPause()
        videoPlayer!!.onVideoPause()
    }

    override fun onResume() {
        super.onResume()
        videoPlayer!!.onVideoResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        mHandler.removeCallbacksAndMessages(null)
        GSYVideoManager.releaseAllVideos()
        if (orientationUtils != null) orientationUtils!!.releaseListener()
    }

    override fun onBackPressed() {
///       不需要回归竖屏
//        if (orientationUtils.getScreenType() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
//            videoPlayer.getFullscreenButton().performClick();
//            return;
//        }
        //释放所有
        videoPlayer!!.setVideoAllCallBack(null)
        super.onBackPressed()
    }
}